package com.lonely.fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-27 23:09
 * @author: ztkj-hzb
 * @description: 事件传递
 **/
public class FilterAndHandlerSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: pink");

        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.setStyle("-fx-background-color: #BBFFFF");
        hBox.setPrefSize(300, 300);

        Button button = new Button("Button");
        Label label = new Label("这是一个文本");

        hBox.getChildren().addAll(button, label);
        hBox.setSpacing(30);
        AnchorPane.setTopAnchor(hBox, 150.0);
        AnchorPane.setLeftAnchor(hBox, 150.0);
        anchorPane.getChildren().addAll(hBox);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(600);
        primaryStage.show();


        //观察filter处理流程，是从父窗口--》子窗口
        anchorPane.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("anchorPane filter ,目标对象：" + event.getTarget());
            }
        });

        hBox.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("hbox filter ,目标对象：" + event.getTarget());
            }
        });

        label.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("label filter ,目标对象：" + event.getTarget());
            }
        });



        //观察 handler流程， 是先触发 子节点--》父节点
        anchorPane.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("anchorPane handler ,目标对象：" + event.getTarget());
            }
        });

        hBox.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("hbox handler ,目标对象：" + event.getTarget());
            }
        });

        label.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("label handler ,目标对象：" + event.getTarget());

                //TODO 使用consume方法可以阻击向下传递
                //event.consume();
            }
        });



        button.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("button filter ,目标对象：" + event.getTarget());
            }
        });

        //TODO 但是注意，Button组件比较特殊，当Button使用 EventHandler时候，不会向下传递
        //本质上 Button.setOnAction ==== button.addEventHandler(MouseEvent.MOUSE_CLICKED)
        button.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("button handler ,目标对象：" + event.getTarget());

                //TODO 使用consume方法可以阻击向下传递
                //event.consume();
            }
        });



    }
}
